install.packages("ggplot2")
install.packages("plyr")

library(ggplot2)
library(plyr)

###############
##paper plots
#

summary_stats<- read.csv("file:///C:/Users/bb372/OneDrive - University of Exeter/lab games/analysis/190528 Summary statistics for R.csv",
                         header=TRUE, sep=",")
sum_stats<-data.frame(summary_stats)
#

DF_Control<-subset(sum_stats,Type == "Control", select = c(1:6))
DF_LC_sim<-subset(sum_stats,Type == "Low cost, simultaneous", select = c(1:6))
DF_LC_seq<-subset(sum_stats,Type == "Low cost, sequential", select = c(1:6))
DF_HC_sim<-subset(sum_stats,Type == "High cost, simultaneous", select = c(1:6))
DF_HC_seq<-subset(sum_stats,Type == "High cost, sequential", select = c(1:6))

df_control_cumsum<-ddply(DF_Control,"Round",transform,label_ypos=cumsum(Decision.number))
df_LC_sim_cumsum<-ddply(DF_LC_sim,"Round",transform,label_ypos=cumsum(Decision.number))
df_LC_seq_cumsum<-ddply(DF_LC_seq,"Round",transform,label_ypos=cumsum(Decision.number))
df_HC_sim_cumsum<-ddply(DF_HC_sim,"Round",transform,label_ypos=cumsum(Decision.number))
df_HC_seq_cumsum<-ddply(DF_HC_seq,"Round",transform,label_ypos=cumsum(Decision.number))

df_control_cumsum_bonus<-subset(df_control_cumsum,Round<=10, select = c(1:7))
df_LC_sim_cumsum_bonus<-subset(df_LC_sim_cumsum,Round<=10, select = c(1:7))
df_LC_seq_cumsum_bonus<-subset(df_LC_seq_cumsum,Round<=10, select = c(1:7))
df_HC_sim_cumsum_bonus<-subset(df_HC_sim_cumsum,Round<=10, select = c(1:7))
df_HC_seq_cumsum_bonus<-subset(df_HC_seq_cumsum,Round<=10, select = c(1:7))

df_control_cumsum_fine<-subset(df_control_cumsum,Round>=11, select = c(1:7))
df_LC_sim_cumsum_fine<-subset(df_LC_sim_cumsum,Round>=11, select = c(1:7))
df_LC_seq_cumsum_fine<-subset(df_LC_seq_cumsum,Round>=11, select = c(1:7))
df_HC_sim_cumsum_fine<-subset(df_HC_sim_cumsum,Round>=11, select = c(1:7))
df_HC_seq_cumsum_fine<-subset(df_HC_seq_cumsum,Round>=11, select = c(1:7))

df_control_cumsum_fine$Round<-df_control_cumsum_fine$Round-10
df_LC_sim_cumsum_fine$Round<-df_LC_sim_cumsum_fine$Round-10
df_LC_seq_cumsum_fine$Round<-df_LC_seq_cumsum_fine$Round-10
df_HC_sim_cumsum_fine$Round<-df_HC_sim_cumsum_fine$Round-10
df_HC_seq_cumsum_fine$Round<-df_HC_seq_cumsum_fine$Round-10



#df_control_cumsum1<-subset(df_control_cumsum,Round==1, select = c(1:7))

##Bonus plots
Control_bars<-ggplot(data = df_control_cumsum_bonus,
                     aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=1.6, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,150))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Standalone group bonus")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+10) ), size=3.5)

LC_sim_bars<-ggplot(data = df_LC_sim_cumsum_bonus,
                    aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=3.5, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: low price, simultaneous")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


LC_seq_bars<-ggplot(data = df_LC_seq_cumsum_bonus,
                    aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=2.6, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: low price, sequential")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


HC_sim_bars<-ggplot(data = df_HC_sim_cumsum_bonus,
                    aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=1.6, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: high price, simultaneous")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


HC_seq_bars<-ggplot(data = df_HC_seq_cumsum_bonus,
                    aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=1.8, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: high price, sequential")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)

##Fine plots

Control_bars_fine<-ggplot(data = df_control_cumsum_fine,
                          aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=1.6, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,150))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Standalone group bonus")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+10) ), size=3.5)

LC_sim_bars_fine<-ggplot(data = df_LC_sim_cumsum_fine,
                         aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=2, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: low price, simultaneous")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


LC_seq_bars_fine<-ggplot(data = df_LC_seq_cumsum_fine,
                         aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=4, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: low price, sequential")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


HC_sim_bars_fine<-ggplot(data = df_HC_sim_cumsum_fine,
                         aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=4, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: high price, simultaneous")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)


HC_seq_bars_fine<-ggplot(data = df_HC_seq_cumsum_fine,
                         aes(x = factor(Round), y = Decision.number, fill = factor(Monitoring)))+
  geom_bar(stat="identity")+
  geom_text(aes(y=label_ypos, label=signif(Number.monitoring,digits = 2)), vjust=1.6, 
            color="white", size=3)+
  scale_fill_manual(values = c("#004949","#009292"))+
  theme_minimal()+scale_y_continuous(limits=c(0,60))+
  theme(axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  labs(x = "Round", y = "Decision number")+ggtitle("Hybrid mechanism: high price, sequential")+
  geom_segment(y=30,yend=30,x=0.5,xend=10.6,linetype=2,size=1.05)+
  geom_text(aes(label=Number.of.groups.that.made.target,x=Round, y = (label_ypos+3) ), size=3.5)

